package com.thesis.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.DocumentInfo;
import com.thesis.manage.domain.Teacher;
import com.thesis.manage.service.DocumentInfoService;
import com.thesis.manage.service.model.DocumentEnum;
import com.thesis.manage.service.module.bean.TaskBook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author php
 * @date 2018/3/28
 */
@RequestMapping("/document")
@RestController
public class DocumentController extends BaseController {
    @Resource
    private DocumentInfoService documentInfoService;

    /**
     * 任务书保存
     *
     * @param studentId 学生号
     * @param taskBook  任务书
     */
    @PostMapping("/taskBook/save")
    public Map<String, Object> saveTaskBook(Long studentId, TaskBook taskBook, @RequestParam(required = false) Long id) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            documentInfoService.saveTaskBook(studentId, teacher.getName(), teacher.getId(), taskBook, id);
            return null;
        });
    }

    /**
     * 查询任务书
     *
     * @param studentId 学生id
     */
    @GetMapping("/{studentId}/taskBook")
    public Map<String, Object> findTaskBook(@PathVariable Long studentId) {
        return buildMessage(() -> documentInfoService.findByStudentId(studentId, DocumentEnum.TASK_BOOK.getValue()));
    }

    /**
     * 开题报告
     */
    @GetMapping("/{studentId}/openReport")
    public Map<String, Object> findOpenReport(@PathVariable Long studentId) {
        return buildMessage(() -> documentInfoService.findOpenReportByStudentId(studentId, DocumentEnum.OPEN_REPORT.getValue()));
    }

    /**
     * 审核开题报告
     *
     * @param oldReport 原文档
     * @param newReport 审核内容文档
     * @param status    通过 ，不通过
     * @param id        原文档id
     */
    @PostMapping("/{id}/review/openReport")
    public Map<String, Object> reviewOpenReport(@RequestParam(required = false) String oldReport, @RequestParam(required = false) String newReport, Integer status, @PathVariable Long id) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            documentInfoService.reviewDocument(oldReport, newReport, status, id, teacher.getId(), teacher.getName());
            return null;
        });
    }

    /**
     * 查询文档
     *
     * @param studentId 学生id
     * @param type      类型
     */
    @GetMapping("/{studentId}/{type}")
    public Map<String, Object> findDocumentByStudentId(@PathVariable Long studentId, @PathVariable Integer type) {
        return buildMessage(() -> documentInfoService.findByStudentId(studentId, type));
    }


    /**
     * 查找论文信息
     */
    @GetMapping("/{studentId}/thesis/info")
    public Map<String, Object> findThesisInfos(@PathVariable Long studentId) {
        return buildMessage(() -> documentInfoService.findThesisInfos(studentId));
    }

    /**
     * 审核论文
     *
     * @param id 文档id
     */
    @PostMapping("/review/thesis")
    public Map<String, Object> reviewThesisInfo(Long id, Integer status, @RequestParam(required = false, defaultValue = "暂无审核内容") String message) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            documentInfoService.reviewThesisInfo(id, teacher.getId(), teacher.getName(), status, message);
            return null;
        });
    }

    /**
     * 通过文档查询审核文档
     *
     * @param id id
     */
    @GetMapping("/{id}")
    public Map<String, Object> findDoucmentByDoucmentId(@PathVariable Long id) {
        return buildMessage(() -> {
            DocumentInfo documentInfo = new DocumentInfo();
            documentInfo.setDocumentId(id);
            return documentInfoService.findByOne(documentInfo);
        });
    }

    /**
     * 查看答辩安排
     */
    @GetMapping("/defence/arrange")
    public Map<String, Object> findDefenceArrange() {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            return documentInfoService.findDefenceArrange(teacher.getCollegeName());
        });
    }

    /**
     * 保存答辩安排
     *
     * @param url url地址
     */
    @PostMapping("/defence/save")
    public Map<String, Object> saveDefenceArrange(String url) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            documentInfoService.saveDefenceArrange(teacher, url);
            return null;
        });
    }
}
